package day240602;
import java.util.ArrayList;
import java.util.Comparator;
public class CollectionCommonMethod5 {
    public static void main(String[] args) {
        ArrayList<Student> students = new ArrayList<>();
        students.add(new Student("小叶", 'Y', 19));
        students.add(new Student("小张", 'Z', 18));
        students.add(new Student("小黄", 'H', 20));

        System.out.println(students);

        //students.sort(new StudentFirstLetterComparator());
        students.sort(new StudentAgeComparator());
        // 回忆之前自己写过的 MySortUtil.sort(...)

        System.out.println(students);
    }
}
class Student {
    String name;
    char firstLetter;
    int age;

    public Student(String name, char firstLetter, int age) {
        this.name = name;
        this.firstLetter = firstLetter;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", firstLetter=" + firstLetter +
                ", age=" + age +
                '}';
    }
}

class StudentFirstLetterComparator implements Comparator<Student> {
    @Override
    public int compare(Student s1, Student s2) {
        if (s1.firstLetter == s2.firstLetter) {
            return 0;
        } else if (s1.firstLetter > s2.firstLetter) {
            return 1;
        } else {
            return -1;
        }
    }
}

class StudentAgeComparator implements Comparator<Student> {
    @Override
    public int compare(Student s1, Student s2) {
        return Integer.compare(s1.age, s2.age);
    }
}